﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Tomato.Allure.Samples
{
	public partial class OpenAllForm
	{
		private delegate void UpdateViewDelegate();

		public void RebuildSolutionFileList()
		{
			m_solutionFiles.Clear();

			for( int i = 0 ; i < m_visualStudio.ActiveSolution.Projects.Count ; ++i )
			{
				IProject project = m_visualStudio.ActiveSolution.Projects[ i ];
				
				AddProjectFiles( project.ProjectItems );
			}

			m_solutionFiles.Sort( new SolutionFileEntry.Comparer() );

#if false
			comboBox1.SuspendLayout();
			comboBox1.AutoCompleteCustomSource.Clear();
			foreach( SolutionFileEntry entry in m_solutionFiles )
			{
				comboBox1.AutoCompleteCustomSource.Add( entry.ProjectItem.Name );
			}
			comboBox1.ResumeLayout();
#endif
		}

		private void AddProjectFiles( IProjectItems projectItems )
		{
			if( projectItems != null )
			{
				for( int i = 0 ; i < projectItems.Count ; ++i )
				{
					IProjectItem projectItem = projectItems[ i ];

					if( projectItem.Kind == ProjectItemKind.PhysicalFile )
					{
						string name = projectItem.Name;
						m_solutionFiles.Add( new SolutionFileEntry( projectItem ) );
					}

					if( projectItem.SubProject != null )
					{
						AddProjectFiles( projectItem.SubProject.ProjectItems );
					}
					else
					{
						AddProjectFiles( projectItem.ProjectItems );
					}
				}
			}
		}

		private void FilterFileList()
		{
            button3.Enabled = false;

            if( !m_bRebuildingFileList )
            {
                listView1.BeginUpdate();
                listView1.Items.Clear();

                string[] filters = m_currentFilterString.ToLower().Split( ' ' );

                List<SolutionFileEntry> itemsToRemove = new List<SolutionFileEntry>();

                m_filteredFileCount = 0;
                foreach( SolutionFileEntry solutionFileItem in m_solutionFiles )
                {
                    try
                    {
                        if( CheckAgainstFilter( solutionFileItem.ProjectItem.Name.ToLower(), filters ) )
                        {
                            ListViewItem item = new ListViewItem( new string[] {
							solutionFileItem.ProjectItem.Name,
							solutionFileItem.ProjectItem.ContainingProject.Name } );
                            item.Tag = solutionFileItem;
                            listView1.Items.Add( item );

                            m_filteredFileCount += 1;
                        }
                    }
                    catch( Exception )
                    {
                        itemsToRemove.Add( solutionFileItem );
                    }
                }

                // remove incorrect items
                foreach( SolutionFileEntry item in itemsToRemove )
                {
                    m_solutionFiles.Remove( item );
                }

                listView1.EndUpdate();
                Text = "OpenAll [" + m_filteredFileCount + " / " + m_solutionFiles.Count + "]";

                if( m_filteredFileCount > 0 )
                {
                    m_lastFilterString = m_currentFilterString;
                }

                if( m_filteredFileCount == 1 )
                {
                    button3.Enabled = true;
                }
            }
		}

		private bool CheckAgainstFilter( string fileName, string[] filters )
		{
			foreach( string token in filters )
			{
				if( !fileName.Contains( token ) )
				{
					return false;
				}
			}

			return true;
		}

		private void OpenSelectedFiles()
		{
            if( listView1.Items.Count == 1 )
            {
                SolutionFileEntry solutionFileItem = listView1.Items[0].Tag as SolutionFileEntry;
                if( solutionFileItem != null )
                {
                    comboBox1.Items.Add( comboBox1.Text );
                    solutionFileItem.Open();
                    return;
                }
            }

			if( listView1.SelectedItems.Count > 0 )
			{
				foreach( ListViewItem selectedItem in listView1.SelectedItems )
				{
					SolutionFileEntry solutionFileItem = selectedItem.Tag as SolutionFileEntry;
					if( solutionFileItem != null )
					{
                        comboBox1.Items.Add( comboBox1.Text );
						solutionFileItem.Open();
					}
				}
			}
		}
	}
}
